package com.jsgt.greekfire.ui.wallet.assets

import android.content.Context
import android.text.InputType
import android.view.View
import android.widget.EditText
import com.fhx.core.base.BaseActivity
import com.fhx.core.utils.QRCodeUtils
import com.fhx.core.utils.ScreenUtil
import com.fhx.core.utils.ToastUtils
import com.google.gson.Gson
import com.jsgt.greekfire.App
import com.jsgt.greekfire.R
import com.jsgt.greekfire.common.widget.maxDecimalPlace
import com.jsgt.greekfire.dao.model.IdentityWallet
import com.jsgt.greekfire.ui.dialog.InputDialogUtil
import com.jsgt.greekfire.ui.dialog.ReceiveQRDialog
import kotlinx.android.synthetic.main.activity_receive.*
import org.jetbrains.anko.find
import java.lang.Exception

class ReceiveActivity : BaseActivity(), View.OnClickListener {
    private var wallet: IdentityWallet? = null
    private val qrMap by lazy { HashMap<String, String>() }
    private val height by lazy { ScreenUtil.dip2px(this, 200f).toInt() }

    override fun needFitsSystemWindows(): Boolean {
        return false
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_receive
    }

    override fun main() {
        setStatusBar(R.color.transparent, false, R.color.white)
        title = getString(R.string.receive)

        wallet = intent.getParcelableExtra("wallet")
        if (wallet == null) {
            finish()
        }

        qrMap["coin"] = wallet!!.key.toString()
        qrMap["address"] = wallet!!.address.toString()

//        val codeBitmap = QRCodeUtils.createQRCodeBitmap(Gson().toJson(qrMap), height, height)
        val codeBitmap = QRCodeUtils.createQRCodeBitmap(wallet!!.address.toString(), height, height)
        imvQR.setImageBitmap(codeBitmap)
        txvAddress.text = wallet!!.address
        txvScanDesc.text = String.format(getString(R.string.receive_desc), wallet!!.key)

        txvAddress.setOnClickListener(this)
        txvAmount.setOnClickListener(this)
        txvSave.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.txvAddress -> {
                val clipboardManager = App.context.getSystemService(Context.CLIPBOARD_SERVICE)
                        as android.text.ClipboardManager
                clipboardManager.text = txvAddress?.text.toString().trim()
                ToastUtils.show(resources.getString(R.string.copied))
            }
            R.id.txvAmount -> {
                InputDialogUtil.showInputDialog(
                    this,
                    getString(R.string.receive_setting_amount),
                    getString(R.string.receive_setting_amount_hint),
                    {
                        val editText = it?.find<EditText>(R.id.edtContent)
                        editText?.inputType =
                            InputType.TYPE_NUMBER_FLAG_DECIMAL or InputType.TYPE_NUMBER_FLAG_DECIMAL
                        editText?.maxDecimalPlace(2)
                    }
                ) {
                    try {
                        it.toDouble()
                        if (it.isNotEmpty()) {
                            txvScanDesc.text =
                                String.format(
                                    getString(R.string.receive_desc),
                                    it + " " + wallet!!.key
                                )
                            qrMap["amount"] = it
                            val codeBitmap =
                                QRCodeUtils.createQRCodeBitmap(Gson().toJson(qrMap), height, height)
                            imvQR.setImageBitmap(codeBitmap)
                        }
                    } catch (error: Exception) {
                    }
                }
            }
            R.id.txvSave -> {
                val dialog = ReceiveQRDialog(wallet)
                dialog.show(supportFragmentManager, "code")
            }
        }
    }
}